package com.wswy.wzcx.model.home;

import android.content.Context;
import android.text.TextUtils;
import com.blankj.utilcode.util.EncryptUtils;
import com.che.libcommon.utils.JsonKit;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.wswy.wzcx.utils.ShareHelper;
import java.util.Map;
import org.json.JSONObject;

public class ExtModule implements Cloneable{


  @SerializedName("title")
  public String title;

  @SerializedName("icon")
  public String image;

  @SerializedName("targetUrl")
  public String targetUrl;

  @SerializedName("targetType")
  public int targetType;

  @SerializedName("tag")
  public String tag;

  @SerializedName("extend")
  public String extend;

  public boolean isNative(){
    return RouterUtils.isNative(targetType);
  }

  public boolean isWeb(){
    return RouterUtils.isWeb(targetType);
  }

  public boolean isTuiaAD(){
    return  targetType == RouterUtils.TYPE_TUIA_AD;
  }

  public boolean isMiniProgram(){
    return targetType == RouterUtils.TYPE_WX_MINI_PROGRAM;
  }

  private transient String spKey;
  private transient int tuidAdId = -1;
  private transient JSONObject extJson;

  public transient String dSign;

  //高度所占的比例
  public transient int weight = 0;

  public String getSpKey(){
    if (spKey == null){
      if (isWeb()){
        spKey = EncryptUtils.encryptMD5ToString(targetUrl+tag);
      }else {
        spKey = targetUrl+tag;
      }
    }
    return spKey;
  }


  private void preExtJson(){
    if (extJson == null){
      try {
        extJson = new JSONObject(extend);
      }catch (Exception e){
        e.printStackTrace();
        extJson =new JSONObject();
      }
    }
  }
  public String getSubTitle(){
    preExtJson();
    return extJson.optString("desc");
  }

  public int getStyle(){
    preExtJson();
    int style = extJson.optInt("style", 0);
    weight = style;
    return style;
  }

  public boolean canShow(Context context){
    if (isWeb() ){
      return true;
    }
    if (isTuiaAD() && getTuaiAdId() > 0){
      return true;
    }
    if (isMiniProgram() && ShareHelper.supportMiniProgram()){
      return true;
    }

    return RouterUtils.hasPage(context,targetUrl);
  }

  public int getTuaiAdId(){
    if (tuidAdId == -1){

      try{
        //推啊广告id区分平台需要手动解析，格式 {"android":"xxx123","ios":"xxx456"}
        if (!TextUtils.isEmpty(targetUrl)){
          JSONObject jsonObject = new JSONObject(targetUrl);
          tuidAdId = jsonObject.optInt("android",0);
        }
      }catch (Exception e){
        e.printStackTrace();
      }
    }
    return tuidAdId;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    ExtModule extModule = (ExtModule) o;

    if (targetType != extModule.targetType) {
      return false;
    }
    if (title != null ? !title.equals(extModule.title) : extModule.title != null) {
      return false;
    }
    if (image != null ? !image.equals(extModule.image) : extModule.image != null) {
      return false;
    }
    if (targetUrl != null ? !targetUrl.equals(extModule.targetUrl) : extModule.targetUrl != null) {
      return false;
    }
    return tag != null ? tag.equals(extModule.tag) : extModule.tag == null;
  }

  @Override
  public int hashCode() {
    int result = title != null ? title.hashCode() : 0;
    result = 31 * result + (image != null ? image.hashCode() : 0);
    result = 31 * result + (targetUrl != null ? targetUrl.hashCode() : 0);
    result = 31 * result + targetType;
    result = 31 * result + (tag != null ? tag.hashCode() : 0);
    return result;
  }


  @Override
  public String toString() {
    return "ExtModule{" +
        "title='" + title + '\'' +
        ", image='" + image + '\'' +
        ", targetUrl='" + targetUrl + '\'' +
        ", targetType=" + targetType +
        ", tag='" + tag + '\'' +
        '}';
  }
}
